package com.da.springtest.ioc.lookup;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 1：ServiceB实现了ApplicationContextAware接口，然后实现了这个接口中的setApplicationContext方法，spring容器在创建ServiceB
 * 的时候会自动调用setApplicationContext方法。
 *
 * 2：从容器中主动去获取ServiceA，这样每次获取到的ServiceA都是一个新的实例。
 *
 * 3：say方法中调用getServiceA方法获取ServiceA对象，然后将其输出。
 */
public class ServiceB implements ApplicationContextAware {

    public void say() {
        ServiceA serviceA = this.getServiceA();
        System.out.println("this:" + this + ",serviceA:" + serviceA);
    }

    public ServiceA getServiceA() {
        return this.context.getBean(ServiceA.class);
    }

    private ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}